package com.foreknow.demo8;

import java.util.*;

public class Test2 {
    public static void main(String[] args) {
        List<Map<String,Student>> list = new ArrayList<>();
        Map<String,Student> map1 = new HashMap<>();
        Student s1 = new Student();
        s1.setSname("tom");
        Student s2 = new Student();
        s2.setSname("fender");
        map1.put("s1",s1);
        map1.put("s2",s2);
        Map<String,Student> map2 = new HashMap<>();
        Student s3 = new Student();
        s3.setSname("xxx");
        Student s4 = new Student();
        s4.setSname("yyy");
        map2.put("s3",s3);
        map2.put("s4",s4);
        //将map集合添加到List集合中
        list.add(map1);
        list.add(map2);
        //遍历输出
        for(int i = 0;i<list.size();i++){
            //获取List中的Map集合
            Map<String,Student> m = list.get(i);
            //通过entrySet返回一个Set集合，集合中保存Map
            Set<Map.Entry<String,Student>> s = m.entrySet();
            //使用迭代器
            Iterator<Map.Entry<String,Student>> it = s.iterator();
            //遍历输出
            while (it.hasNext()){
                //获取到Map中的元素
                Map.Entry entry = it.next();
                //通过 Map.Entry getValue()方法获取到值(Student对象)，为什么强转：getValue返回Object
                Student student = (Student) entry.getValue();
                //通过 Map.Entry中的getKey()方法获取到key
                System.out.println(entry.getKey()+"----"+student.getSname());
            }
        }
    }
}
